//
//  MoneyPicker.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MoneyPicker.h"
#import "SetPredictViewController.h"
@interface MoneyPicker()<UIPickerViewDataSource,UIPickerViewDelegate>

@end
@implementation MoneyPicker
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
        [self addSubview:self.pickerView];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
    }
    return self;
}
- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, kSCREEN_WIDTH, 260)];
        [self addSubview:_pickerView];
        UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 40)];
        toolBar.autoresizingMask =  UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
        
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(actionCancel:)];
        UIBarButtonItem *confirmItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(actionDone:)];
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        space.width = kSCREEN_WIDTH - 100;
        [toolBar setItems:[NSArray arrayWithObjects:cancelItem,space,confirmItem,nil]];
        [self addSubview:toolBar];
       
    }
    return _pickerView;
}

//取消
- (void)actionCancel:(UIBarButtonItem *)item {
    if (self.DateSelected) {
        self.DateSelected(@"取消");
    }
}
//完成
- (void)actionDone:(UIBarButtonItem *)item {
    if (self.DateSelected) {
        self.DateSelected(@"完成");
    }
}
- (void)setUpUI {
    self.moneyArray = @[@"1",
                        @"2",
                        @"3",
                        @"4",
                        @"5",
                        @"6",
                        @"7",
                        @"8",
                        @"9",
                        @"10",
                        ];
}

#pragma mark -- PickerViewDelegate DataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.moneyArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.moneyArray[row];
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    // 这里用label来展示要显示的文字, 然后可以用label的textAlignment来设置文字的对齐模式
    UILabel *myView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 40)];
    myView.font = [UIFont systemFontOfSize:18];
    myView.backgroundColor = [UIColor whiteColor];
    myView.textAlignment = NSTextAlignmentCenter;
    myView.text = self.moneyArray[row];
    return myView;
}
@end
